package backTrack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class subsets {
    List<List<Integer>> list = new ArrayList();
    HashSet<List<Integer>> set = new HashSet<>();
    public List<List<Integer>> subsets(int[] nums) {
        int length = nums.length;
        boolean[] mark = new boolean[length];
        for (int i = 1; i <=length; i++) {
            // 表示长度
            for (int j = 0; j < length; j++) {
                // 表示从哪个开始
                if (i==length) {
                    ArrayList<Integer> sublist = new ArrayList<>();
                    for(int num: nums){
                        sublist.add(num);
                    }
                    list.add(sublist);
                    break;
                }
                backTrack(i, mark, nums, j, new ArrayList<>());

            }
        }
        list.add(new ArrayList<>());
        list.addAll(set);
        return list;
    }

    public void backTrack(int count, boolean[] mark, int[] nums, int curr, List<Integer> subList) {
        mark[curr] = true;
        subList.add(nums[curr]);
        if (count == 1) {
            // list.add(new ArrayList<>(subList));
            //如何去重
            ArrayList<Integer> arrayList = new ArrayList<>(subList);
            Collections.sort(arrayList);

            set.add(new ArrayList<>(arrayList));
            mark[curr]=false;
            subList.remove(subList.size()-1);
            return;
        }
        
        for (int i = 0; i < nums.length; i++) {
            if (!mark[i]) {
                backTrack(count - 1, mark, nums, i, subList);
            }
        }
        mark[curr] = false;
        subList.remove(subList.size() - 1);
    }

    public static void main(String[] args) {
        subsets obj = new subsets();
        int[] nums = new int[]{3,2,1};
        List<List<Integer>> lists = obj.subsets(nums);
        for(List<Integer>list:lists){
            for(Integer i:list){
                System.out.println(i);
            }
            System.out.println("============");
        }
    }
}
